package com.ruoyi.common.service.business.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.pagination.Paging;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.common.core.pagination.PageInfo;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.param.ProductCategoryParam;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.vo.business.ProductCategoryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.mapper.business.ProductCategoryMapper;
import com.ruoyi.common.domain.business.ProductCategory;
import com.ruoyi.common.service.business.ProductCategoryService;

/**
 * 产品分类Service业务层处理
 *
 * @author Song
 * @date 2024-10-29
 */
@Service
public class ProductCategoryServiceImpl extends ServiceImpl<ProductCategoryMapper, ProductCategory> implements ProductCategoryService {

    @Autowired
    private ProductCategoryMapper productCategoryMapper;

    /**
     * 查询产品分类列表
     *
     * @param productCategory 产品分类
     * @return 产品分类
     */
    public Paging<ProductCategoryVo> pageProductCategory(ProductCategoryParam productCategory) {
        LambdaQueryWrapper<ProductCategory> wrapper = new LambdaQueryWrapper<ProductCategory>();
        if (StringUtils.isNotBlank(productCategory.getName())){
            wrapper.like(ProductCategory::getName,productCategory.getName());
        }
        wrapper.orderByAsc(ProductCategory::getSort);
        Page<ProductCategory> page = new PageInfo<>(productCategory);

        IPage<ProductCategory> iPage = this.page(page, wrapper);
        List<ProductCategory> categoryList = productCategoryMapper.selectAllList();

        // 最终返回分页对象
        IPage<ProductCategoryVo> resultPage = iPage.convert(item -> {
            ProductCategoryVo vo = new ProductCategoryVo();
            BeanUtil.copyProperties(item, vo);
            if (vo.getParentId()==0L){
                vo.setParentName("顶级分类");
            }else {
                vo.setParentName(categoryList.stream().filter(o-> Objects.equals(o.getId(), item.getParentId())).collect(Collectors.toList()).get(0).getName());
            }
            return vo;
        });
        return new Paging(resultPage);
    }

    public void addCategory(ProductCategory productCategory) {
        this.save(productCategory);
    }

    public List<ProductCategoryVo> getList() {
        List<ProductCategory> categories = productCategoryMapper.selectAllList();
        List<ProductCategoryVo> result = categories.stream().map(item -> {
            ProductCategoryVo vo = new ProductCategoryVo();
            BeanUtil.copyProperties(item, vo);
            return vo;
        }).collect(Collectors.toList());
        return result;
    }

    public ProductCategoryVo toEdit(Long id) {
        ProductCategory category = getById(id);
        ProductCategoryVo vo = new ProductCategoryVo();
        if (category!=null){
            BeanUtil.copyProperties(category,vo);
        }
        return vo;
    }


    public void edit(ProductCategory productCategory) {
        updateById(productCategory);
    }

    public void removeCategory(Long id) {
        long count = count(new LambdaQueryWrapper<ProductCategory>().eq(ProductCategory::getParentId, id));
        if (count>0){
            throw new ServiceException("该分类尚有下级分类，删除失败");
        }
        removeById(id);
    }

    public List<Tree<Long>> treeList() {
        List<ProductCategory> productCategories = productCategoryMapper.selectAllList();
        List<TreeNode<Long>> treeNodeList = productCategories.stream().map(o -> {
            TreeNode<Long> longTreeNode = new TreeNode<>(o.getId(), o.getParentId(), o.getName(), o.getId());
            HashMap<String, Object> map = new HashMap<>();
            longTreeNode.setExtra(map);
            return longTreeNode;
        }).collect(Collectors.toList());
        return TreeUtil.build(treeNodeList,0L);
    }
}



